// pages/lifeService/managementFee/index.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "物业缴费", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    roomList: [{
      id: -1,
      name: '选择房号'
    },{
      id: 0,
      name: '天星月影5栋2单元1102'
    }, {
      id: 1,
      name: '澜语溪岸3栋4单元1801'
    }],
    room: "天星月影5栋2单元1102",
    feeType: 0, //0表示物业费 1表示其他费
    allChoose: false, //true表示全选
    totalFee: 0, //勾选的费用总额
    feeList: [{
        year: '2024',
        choose: false,
        childList: [{
            month: '12月',
            fee: '100.5',
            isArrears: true,
            choose: false
          },
          {
            month: '11月',
            fee: '200',
            isArrears: true,
            choose: false
          }
        ]
      },
      {
        year: '2023',
        choose: false,
        childList: [{
            month: '12月',
            fee: '100',
            isArrears: true,
            choose: false
          },
          {
            month: '11月',
            fee: '200',
            isArrears: true,
            choose: false
          }
        ]
      }
    ]
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  //跳转到缴费记录
  recordList(){
    wx.navigateTo({
      url: '/pages/lifeService/managementFee/recordList',
    })
  },
  roomChange(e) {
    let that = this;
    that.setData({
      room: that.data.roomList[e.detail.value].name,
    })
  },
  //点击物业费
  wyf() {
    let that = this;
    that.setData({
      feeType: 0
    })
  },
  //点击其他费
  qtf() {
    let that = this;
    that.setData({
      feeType: 1
    })
  },
  //全选
  allChoose() {
    let that = this;
    let allChoose = that.data.allChoose;
    let feeList = that.data.feeList;
    allChoose = !allChoose;
    if (allChoose) {
      for (let i = 0; i < feeList.length; i++) {
        feeList[i].choose = true;
        for (let j = 0; j < feeList[i].childList.length; j++) {
          feeList[i].childList[j].choose = true;
        }
      }
    }
    if (!allChoose) {
      for (let i = 0; i < feeList.length; i++) {
        feeList[i].choose = false;
        for (let j = 0; j < feeList[i].childList.length; j++) {
          feeList[i].childList[j].choose = false;
        }
      }
    }
    that.tatalFee();
    that.setData({
      feeList,
      allChoose
    })
  },
  //选择整年的费用
  chooseYear(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let feeList = that.data.feeList;
    let allChoose = that.data.allChoose;
    feeList[index].choose = !feeList[index].choose;
    if (feeList[index].choose) {
      for (let i = 0; i < feeList[index].childList.length; i++) {
        feeList[index].childList[i].choose = true;
      }
    }
    if (!feeList[index].choose) {
      for (let i = 0; i < feeList[index].childList.length; i++) {
        feeList[index].childList[i].choose = false;
        allChoose = false;
      }
    }
    that.tatalFee();
    that.setData({
      feeList,
      allChoose
    })
  },
  goPayList(){
    wx.navigateTo({
      url: '/pages/lifeService/managementFee/payList',
    })
  },
  //选择某个月的费用
  chooseMonth(e) {
    let that = this;
    let childIndex = e.currentTarget.dataset.childindex;
    let parentIndex = e.currentTarget.dataset.parentindex;
    let feeList = that.data.feeList;
    let allChoose = that.data.allChoose;
    feeList[parentIndex].childList[childIndex].choose = !feeList[parentIndex].childList[childIndex].choose;
    if (!feeList[parentIndex].childList[childIndex].choose) {
      feeList[parentIndex].choose = false;
      allChoose = false;
    }
    that.tatalFee();
    that.setData({
      feeList,
      allChoose
    })
  },
  //统计勾选的欠费金额
  tatalFee() {
    let that = this;
    let feeList = that.data.feeList;
    let totalFee = 0;
    for (let i = 0; i < feeList.length; i++) {
      for (let j = 0; j < feeList[i].childList.length; j++) {
        if (feeList[i].childList[j].choose) {
          totalFee += parseFloat(feeList[i].childList[j].fee);
        }
      }
    }
    that.setData({
      totalFee: totalFee
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})